﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Documents;
using System.Windows.Media;

namespace AWZhome.NQ.CommonServices.Visual
{
	/// <summary>
	/// Interaction logic for BandCustomizeWindow.xaml
	/// </summary>
	public partial class BandCustomizeWindow : Window
	{
		private class BandItemListEntry : DependencyObject
		{
			// Using a DependencyProperty as the backing store for IsVisible.  This enables animation, styling, binding, etc...
			public static readonly DependencyProperty IsVisibleProperty =
					DependencyProperty.Register("IsVisible", typeof(bool), typeof(BandItemListEntry), new UIPropertyMetadata(true));

			// Using a DependencyProperty as the backing store for ItemImage.  This enables animation, styling, binding, etc...
			public static readonly DependencyProperty ItemImageProperty =
					DependencyProperty.Register("ItemImage", typeof(ImageSource), typeof(BandItemListEntry), new UIPropertyMetadata(null));

			// Using a DependencyProperty as the backing store for ItemText.  This enables animation, styling, binding, etc...
			public static readonly DependencyProperty ItemTextProperty =
					DependencyProperty.Register("ItemText", typeof(string), typeof(BandItemListEntry), new UIPropertyMetadata(String.Empty));

			// Using a DependencyProperty as the backing store for ItemKey.  This enables animation, styling, binding, etc...
			public static readonly DependencyProperty ItemKeyProperty =
					DependencyProperty.Register("ItemKey", typeof(string), typeof(BandItemListEntry), new UIPropertyMetadata(String.Empty));

			public string ItemKey
			{
				get
				{
					return (string) GetValue(ItemKeyProperty);
				}
				set
				{
					SetValue(ItemKeyProperty, value);
				}
			}

			public string ItemText
			{
				get
				{
					return (string) GetValue(ItemTextProperty);
				}
				set
				{
					SetValue(ItemTextProperty, value);
				}
			}

			public ImageSource ItemImage
			{
				get
				{
					return (ImageSource) GetValue(ItemImageProperty);
				}
				set
				{
					SetValue(ItemImageProperty, value);
				}
			}

			public bool IsVisible
			{
				get
				{
					return (bool) GetValue(IsVisibleProperty);
				}
				set
				{
					SetValue(IsVisibleProperty, value);
				}
			}
		}

		private BarService _servBar;
		private List<BandItemListEntry> _entryList = new List<BandItemListEntry>();

		private NQBarBand band;

		public BandCustomizeWindow()
		{
			InitializeComponent();
		}

		internal BandCustomizeWindow(BarService servBar, NQBarBand band)
			: this()
		{
			this._servBar = servBar;
			this.band = band;

			// Create a collection of BandItemListEntry objects and bind them to window
			foreach (string itemKey in band.GetItemKeys())
			{
				IBarItem item = _servBar.GetItem(itemKey);
				BandItemListEntry listEntry = new BandItemListEntry()
				{
					ItemKey = itemKey,
					ItemImage = item.ItemImage,
					IsVisible = item.ItemVisible
				};
				if (item.ItemType == BarItemType.Separator)
				{
					listEntry.ItemText = "-----------------------------------";
				}
				else if ((item.ItemType == BarItemType.ComboBox) || (item.ItemType == BarItemType.TextBox))
				{
					listEntry.ItemText = item.ItemToolTip;
				}
				else
				{
					if (String.IsNullOrEmpty(item.ItemText))
					{
						listEntry.ItemText = item.ItemToolTip;
					}
					else
					{
						listEntry.ItemText = item.ItemText;
					}
				}
				_entryList.Add(listEntry);
			}
			this.ItemList.ItemsSource = _entryList;
		}

		private void CloseButton_Click(object sender, RoutedEventArgs e)
		{
			this.Close();
		}

		private void OKButton_Click(object sender, RoutedEventArgs e)
		{
			// Set the visibility settings for the bar items
			foreach (BandItemListEntry itemEntry in _entryList)
			{
				if (_servBar.ItemExists(itemEntry.ItemKey))
				{
					IBarItem item = _servBar.GetItem(itemEntry.ItemKey);
					item.ItemVisible = itemEntry.IsVisible;
				}
			}
			this.Close();
		}
	}
}
